<?php

namespace App\Response;

/**
 * 用户服务的响应对象
 */
class AdminServiceResponse extends ServiceResponse
{
    public $error;

    public $message;

    public $result;

    public function __construct($response)
    {
        $this->error = $response["error_code"];
        $this->message = $response["error_msg"];
        $this->result = $response["data"];
    }

    public function isOk(): bool
    {
        return $this->error == 0 ;
    }

    public function getMessage(): string
    {
        return $this->message;
    }

    public function getData()
    {
        return $this->result;
    }

    /**
     * 将结果转换为api 的数据结构
     * @return Response
     */
    public function getResponse() :Response
    {
        return new Response(
            $this->error > 0 ? $this->error : 0 ,
            $this->getMessage() ?: ($this->error > 0 ? "not ok" : "ok"),
            $this->getData() ? : [],
        );
    }
}